package com.jrx.anytxn.reporting.job.xib.step;

import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.customer.entity.CmLimitChangeLog;
import com.jrx.anytxn.customer.mapper.ext.ExtCmLimitChangeLogMapper;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.entity.CmInteCoLonCrdtInfo;
import com.jrx.anytxn.reporting.entity.CmInteCoLonInfo;
import com.jrx.anytxn.reporting.entity.CmInteCoLonInfoBean;
import com.jrx.anytxn.reporting.entity.LimitChangeLogUpdate;
import com.jrx.anytxn.reporting.mapper.base.CmInteCoLonInfoMapper;
import com.jrx.anytxn.reporting.mapper.ext.ExtCmInteCoLonCrdtInfoMapper;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.mapper.ext.ExtAmLoanAccountInfoMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class LonInfoWriter implements ItemWriter<CmInteCoLonInfoBean> {

    @Resource
    private CmInteCoLonInfoMapper cmInteCoLonInfoMapper;
    @Resource
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;
    @Resource
    private ExtCmLimitChangeLogMapper extCmLimitChangeLogMapper;
    @Resource
    private IOrganizationTableService organizationTableService;
    @Resource
    ExtCmInteCoLonCrdtInfoMapper extCmInteCoLonCrdtInfoMapper;

    @Override
    public void write(List<? extends CmInteCoLonInfoBean> items) {
        if(CollectionUtils.isNotEmpty(items)){
            //获取当期业务时间
            PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
            Date etlDt = org.getToday();
            for(CmInteCoLonInfoBean bean : items){
                CmInteCoLonInfo info = bean.getCmInteCoLonInfo();
                if (Objects.nonNull(info)) {
                    // 需要修改余额为0的结清数据的数据
                    List<String> loanIds = info.getLoanIds();
                    if (CollectionUtils.isNotEmpty(loanIds)) {
                        // 将结清的数据余额设置为0
                        extAmLoanAccountInfoMapper.updateLoanAcctPrinBal(info.getTenantId(), loanIds);
                    }
                    cmInteCoLonInfoMapper.insertSelective(info);
                }
                LimitChangeLogUpdate updateLimitChangeLog = bean.getUpdateLimitChangeLog();
                if (Objects.nonNull(updateLimitChangeLog)) {
                    CmLimitChangeLog changeLog = BeanMapping.copy(updateLimitChangeLog, CmLimitChangeLog.class);
                    // 更新结清日期
                    extCmLimitChangeLogMapper.updateEtlSettleDate(changeLog);
                    // 需要修改客户额度信息的额度失效日期
                    if (Objects.nonNull(changeLog.getEtlExpiryDate())) {
                        CmInteCoLonCrdtInfo crdtInfo = new CmInteCoLonCrdtInfo();
                        crdtInfo.setCustNo(changeLog.getCustomerId());
                        crdtInfo.setTenantId(changeLog.getTenantId());
                        crdtInfo.setInvalidDt(changeLog.getEtlExpiryDate());
                        crdtInfo.setEtlDt(etlDt);
                        // 修改贷款授信信息额度失效日期
                        extCmInteCoLonCrdtInfoMapper.updateInvalidDt(crdtInfo);
                    }
                }
            }
        }
    }
}
